<?php
Class Login extends Controller
{
  function login()
  {
    parent::Controller();
    $this->load->helper(array('url','form'));
    
    $this->load->library(array('form_validation','session'));
    $this->load->model('user_model');
    
  }
  
  function index()
  {
    $user_ses = $this->session->userdata('userlogindetails');
      if($user_ses['user_id'])
      {
      $this->session->unset_userdata('userlogindetails');
      }
      
    $displayform = '';
    
    $displayform .= '<br/>';
    $displayform .= '<div id="login">';
    $displayform .= '<fieldset ><legend>Login</legend>';   
    $displayform .='<form action="login" id="login_form" method="post" >';
    $displayform .='<table align="center">';
    
    $displayform .='<tr>';
    $displayform .= '<td rowspan="3">';
    $displayform .= '<img src="'.base_url().'images/login_icon.gif" width=100 alt="login">';    
    $displayform .= '</td>';
    $displayform .='<td>';
    $displayform .= '<label>Username:</label>';
    $displayform .='</td>';
    $displayform .='<td>';
    $displayform .= '<input type="text" name="username" id="username" class="required email" />';
    $displayform .='</td>';
    $displayform .='</tr>';
    
    $displayform .='<tr>';
    $displayform .='<td>';
    $displayform .= '<label>Password:</label>';
    $displayform .='</td>';
    $displayform .='<td>';
    $displayform .= '<input type="password" name="password" class="required" />';
    $displayform .='</td>';
    $displayform .='</tr>';
    
    $displayform .='<tr>';
    $displayform .='<td colspan=2 align="right">';
    $displayform .= '<input type="submit" name="submit" value="Login">&nbsp;';
    $displayform .= '<input type="reset" name="submit" value="Cancel">&nbsp;';
     $displayform .='</td>';
    $displayform .='</tr>';
    
    $displayform .='</table>';
    
    $displayform .= '</form>';
    
    if($this->session->userdata('invalid'))
    {
      $displayform .= "<span style='color:red;'>Please check Username and Password</span>";
    }
    $this->session->unset_userdata('invalid');
    
    $displayform .= '</fieldset>';
    $displayform .= '</div>';
    $data = array('formdisplay'=>$displayform );  
    $this->form_validation->set_rules('username', 'User Name', 'trim|required|valid_email|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean'); 
    
       $this->template->add_js('$(document).ready(function(){
                
          window.history.forward();
                                           
             $("#login_form").validate();
              });
              
              
              
        ','embed');
        
           $this->template->add_js('
              $(window).load(function () {
                  $("#username").focus();
              });
         ','embed');
     
        
      if ($this->form_validation->run() == FALSE)
    {
        $this->template->write_view('header','header', $data, True);
        $this->template->write_view('content', 'general');
        $this->template->render();
    }
    else
    {
        $this->user_model->validate_user();
    }
  
  }
  
  
}
